package com.amarsoft.apot.service.impl;

import com.alipay.api.internal.util.StringUtils;
import com.amarsoft.apot.annotation.OperationAnnotation;
import com.amarsoft.apot.mapper.WorkflowCodeSeqDao;
import com.amarsoft.apot.service.UserService;
import com.amarsoft.apot.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * @Description:
 * @title: UserServiceImpl
 * @Author szh
 * @Date: 2021/12/27 14:30
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;

    @Autowired
    private TransactionDefinition transactionDefinition;


    @Autowired
    private WorkflowCodeSeqDao workflowCodeSeqDao;


    private static int flag = 0;

    @Resource
    private RedisUtil redisUtil;


    @Override
    @OperationAnnotation
    public void test(Map paramMap) {
        System.out.println("=======service======"+paramMap);
    }

    @Override
    public void test02(Map paramMap) {


//        // 先查询，然后+1
//        Integer markSeqById = workflowCodeSeqDao.getMarkSeqById(2);


        //System.out.println("================szh==========markSeqById的值是:"+markSeqById);


        //redisUtil.set("name",name);

        String value = redisUtil.get("name").toString();
        if(StringUtils.isEmpty(value)) {
            redisUtil.set("name","2");
        } else {

            while(!StringUtils.isEmpty(value)) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
                value = redisUtil.get("name").toString();
            }

        }
        //事务操作类
        TransactionStatus transactionStatus = null;
        try {
            //开启事务
            transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);

            System.out.println("222222:"+transactionStatus);

            // 先查询，然后+1
            Integer markSeqById = workflowCodeSeqDao.getMarkSeqById(2);
            System.out.println(LocalDateTime.now()+"================szh==========markSeqById的值是:"+markSeqById);

            // +1
            workflowCodeSeqDao.updateMarkSeqSequenceById(2,markSeqById+1);

            //提交事务
            dataSourceTransactionManager.commit(transactionStatus);

        } catch (Exception e) {

        } finally {
            redisUtil.del("name");
        }


    }


}
